package encore.beans.impl;

import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;

import encore.beans.BeanFactory;
import encore.beans.BeansException;
import encore.web.ContextLoader;

public class SpringBeanFactory implements BeanFactory {
    private ApplicationContext context;
    
    public SpringBeanFactory(ServletContext servletContext)
    {
    	Object object=servletContext.getAttribute(ContextLoader.PORTALCONTEXTID);
    	if(object==null)
    		throw new IllegalArgumentException("ServletContext.getAttribute("+ContextLoader.PORTALCONTEXTID+")为空!");
    	context=(ApplicationContext)object;
    }
    

	@Override
	public boolean containBean(String name) {
		// TODO Auto-generated method stub
		return context.containsBean(name);
	}

	@Override
	public String[] getAliases(String name) {
		// TODO Auto-generated method stub
		return context.getAliases(name);
	}

	@Override
	public <T> T getBean(String name, Class<T> requiredType)
			throws BeansException {
		// TODO Auto-generated method stub
		return context.getBean(name,requiredType);
	}

	@Override
	public <T> T getBean(Class<T> requiredType) throws BeansException {
		// TODO Auto-generated method stub
		return context.getBean(requiredType);
	}

	@Override
	public Object getBean(String name, Object... args) throws BeansException {
		return context.getBean(name,args);
	}


	@Override
	public Object getBean(String name) throws BeansException {
		// TODO Auto-generated method stub
		return context.getBean(name);
	}   

}
